package com.wm.task1.server;

import com.sun.source.tree.NewArrayTree;
import com.wm.task1.server.dao.ComputeDAO;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class NetServer implements Runnable{
    private ComputeDAO computeDAO = new ComputeDAO();
    private void newServer() {
        try {
            ServerSocket serverSocket = new ServerSocket(8866);
            System.out.println("等待链接....");
            Socket socket = serverSocket.accept();
            System.out.println("链接成功");
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            String str = (String) ois.readObject();

            Integer result = computeDAO.compute(str);

            oos.writeObject(result);
            oos.flush();

            oos.close();
            ois.close();
            socket.close();
            serverSocket.close();



        } catch (Exception e) {
        }
    }
    public void startThread(){
        Thread th = new Thread(this);
        th.start();
    }
    public void run(){
        newServer();
    }

}
